package com.lilin.gps.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.format.Time;

public class DateUtils {
	public static String getCurTime() {
		return convertDate(new Date(), "HH:mm");
	}

	public static String getCurrentTime() {
		return convertDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	public static String getCurDate() {
		return convertDate(new Date(), "yyyy-MM-dd");
	}

	public static String getCurDate(Date date) {
		return convertDate(date, "yyyy-MM-dd");
	}

	public static Date convertString2Date(String str, String formatStr) {
		DateFormat format = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String convertDate(Date date, String format) {
		if (date != null) {
			DateFormat format1 = new SimpleDateFormat(format);
			String s = format1.format(date);
			return s;
		}
		return "";
	}

	// @SuppressWarnings("static-access")
	// public static String getDateFileName() {
	// // 获取系统时间，并格式化
	// Calendar calendar = Calendar.getInstance();
	// calendar.setTime(new Date());
	// int year = calendar.get(Calendar.YEAR);
	// int mounth = calendar.get(Calendar.MONTH) + 1;
	// int day = calendar.get(Calendar.DATE);
	// int hour = calendar.get(Calendar.HOUR_OF_DAY);// 24小时制
	// // int hour = calendar.get(Calendar.HOUR);12小时制
	// int min = calendar.get(calendar.MINUTE);
	// int secound = calendar.get(calendar.SECOND);
	// // 格式一：2011-11-1 12-12-66 作为文件名
	// String formateDate1 = String.valueOf(year) + "-" + AddZero(mounth)
	// + "-" + AddZero(day) + "  " + AddZero(hour) + "-"
	// + AddZero(min) + "-" + AddZero(secound);
	// return formateDate1;
	// }

	public static String AddZero(int i) {
		if (i >= 0 && i <= 9) {
			return "0" + i;
		}
		return String.valueOf(i);

	}

//	public static String getFormateDate(int type, Date date) {
//		// 获取系统时间，并格式化
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
//		int year = calendar.get(Calendar.YEAR);
//		int mounth = calendar.get(Calendar.MONTH) + 1;
//		int day = calendar.get(Calendar.DATE);
//		int hour = calendar.get(Calendar.HOUR_OF_DAY);
//		int min = calendar.get(calendar.MINUTE);
//		int secound = calendar.get(calendar.SECOND);
//
//		// 格式一：2011-11-1 12-12-66+mima 作为图片文件名
//		if (type == 1) {
//			String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
//			Random random = new Random();
//			int i = random.nextInt(10);
//			String extra = a[i];
//			String mima = MiMaHelp.jiami(extra + AddZero(min)
//					+ AddZero(secound) + random.nextInt(10), 6);
//			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
//					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
//					+ "-" + AddZero(secound) + " " + mima;
//		}
//		// 格式二：2011年11月1日 12时12分45秒 写在照片上
//		else if (type == 2) {
//			return String.valueOf(year) + "年" + AddZero(mounth) + "月"
//					+ AddZero(day) + "日  " + AddZero(hour) + "时" + AddZero(min)
//					+ "分" + AddZero(secound) + "秒";
//		}
//		// 格式三：2011-11-1 12-12-11 作为录音文件名
//		else if (type == 3) {
//			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
//					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
//					+ "-" + AddZero(secound) + " ";
//		}
//		return null;
//	}

//	public static String getFormateDate(int type) {
//		// 获取系统时间，并格式化
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		int year = calendar.get(Calendar.YEAR);
//		int mounth = calendar.get(Calendar.MONTH) + 1;
//		int day = calendar.get(Calendar.DATE);
//		int hour = calendar.get(Calendar.HOUR_OF_DAY);
//		int min = calendar.get(calendar.MINUTE);
//		int secound = calendar.get(calendar.SECOND);
//
//		// 格式一：2011-11-1 12-12-66+mima 作为图片文件名
//		if (type == 1) {
//			String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
//			Random random = new Random();
//			int i = random.nextInt(10);
//			String extra = a[i];
//			String mima = MiMaHelp.jiami(extra + AddZero(min)
//					+ AddZero(secound) + random.nextInt(10), 6);
//			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
//					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
//					+ "-" + AddZero(secound) + " " + mima;
//		}
//		// 格式二：2011年11月1日 12时12分45秒 写在照片上
//		else if (type == 2) {
//			return String.valueOf(year) + "年" + AddZero(mounth) + "月"
//					+ AddZero(day) + "日  " + AddZero(hour) + "时" + AddZero(min)
//					+ "分" + AddZero(secound) + "秒";
//		}
//		// 格式三：2011-11-1 12-12-11 作为录音文件名
//		else if (type == 3) {
//			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
//					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
//					+ "-" + AddZero(secound) + " ";
//		}
//		return null;
//	}

	public static String getTimeFromDate(Date date, int type) {
		String year = formatTimeStr(String.valueOf(date.getYear()));
		String month = formatTimeStr(String.valueOf(date.getMonth()));
		String day = formatTimeStr(String.valueOf(date.getDate()));
		String hours = formatTimeStr(String.valueOf(date.getHours()));
		String minutes = formatTimeStr(String.valueOf(date.getMinutes()));
		String seconds = formatTimeStr(String.valueOf(date.getSeconds()));
		if (type == 1) {
			return year + "." + month + "." + day + " " + hours + ":" + minutes
					+ ":" + seconds;
		} else if (type == 2) {
			return year + "-" + month + "-" + day + " " + hours + ":" + minutes
					+ ":" + seconds + ":";
		} else {
			return year + "年" + month + "月" + day + "日" + hours + "时" + minutes
					+ "分" + seconds + "秒";
		}
	}

	public static String TimeFormat(long dateTaken) {
		return android.text.format.DateFormat.format("yyyy.MM.dd kk:mm:ss",
				dateTaken).toString();
	}

	public static String TimeFormat2(long dateTaken) {
		return android.text.format.DateFormat.format("yyyyMMddkkmmss",
				dateTaken).toString();
	}

	/**
	 * 获取时间字符串
	 * 
	 * @return
	 */
	public static String getTimeStr(int type) {
		Time t = new Time();
		t.setToNow();
		String yearStr = formatTimeStr(String.valueOf(t.year));
		String monthStr = formatTimeStr(String.valueOf(t.month + 1));
		String dayStr = formatTimeStr(String.valueOf(t.monthDay));
		String hourStr = formatTimeStr(String.valueOf(t.hour));
		String minuteStr = formatTimeStr(String.valueOf(t.minute));
		String secondStr = formatTimeStr(String.valueOf(t.second));
		if (type == 1) {
			return yearStr + "年" + monthStr + "月" + dayStr + "日" + hourStr
					+ "时" + minuteStr + "分" + secondStr + "秒";
		} else {
			return yearStr + monthStr + dayStr + hourStr + minuteStr
					+ secondStr;
		}
	}

	/**
	 * 格式化时间字符串
	 * 
	 * @param timeStr
	 * @return
	 */
	public static String formatTimeStr(String timeStr) {
		if (timeStr.length() == 1) {
			return "0" + timeStr;
		} else {
			return timeStr;
		}
	}

	// 获得格式化的日期
	public static String getFormateDate2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
		return sdf.format(new Date());
	}

}
